let search = require('../api/kuwo/search');
let musicList = require('../api/kuwo/musicList');
let albumInfo = require('../api/kuwo/albumInfo');
let singer = require('../api/kuwo/singer');
let playUrl = require('../api/kuwo/playUrl');
let lrc = require('../api/kuwo/lrc');
export async function getKwSearch(keywords,pn,rn,type,f){
  let result;
  switch (type){
    case '0':
    case 0:
      //console.log(11)
      await search.prototype.searchMusicBykeyWord({
        key:encodeURI(keywords),
        pn : pn,
        rn : rn
      }).then((r) => {
        //console.log(r);
        result = dealSearchSongResult(r.data?.list)
        let count = r.data?.total;
        result = {v:result, count:parseInt(count)};
      })

      break;
    case '1':
    case 1 :
      await search.prototype.searchArtistBykeyWord({
        key:encodeURI(keywords),
        pn : 1,
        rn : 30
      }).then((r) => {
        result = dealSearchSingerResult(r.data?.list)
      })
      break;
    case '2':
    case 2 :
      await search.prototype.searchPlayListBykeyWord({
        key:encodeURI(keywords),
        pn : 1,
        rn : 30
      }).then((r) => {
        result = dealSearchPlayListResult(r.data?.list)
      })
      break;
    case '3' :
    case 3 :
      await search.prototype.searchAlbumBykeyWord({
        key:encodeURI(keywords),
        pn : 1,
        rn : 30
      }).then((r) => {
        result = dealSearchAlbumResult(r.data)
      })
      break;
  }
  f(result);
}

function dealSearchAlbumResult(data){
  //console.log(data)
  let als = data.albumList || []
  return als.map((r) => {

    return {
      "name":r.album,
      "id":r.albumid,
      "img":r.pic,
      "auth":r.artist,
      "from":'kuwo'
    }

  })



}
function dealSearchPlayListResult(data){

  let als = data || []
  return als.map((r) => {

    return {
      "name":r.name,
      "id":r.id,
      "imgUrl":r.img,
      "auth":r.uname,
      "from":'kuwo'
    }

  })

}


function dealSearchSongResult(data) {


  let tracks = data;
  let r = tracks.map((i) => {
    let times = i.songTimeMinutes;
    times = times.replace(":"," : ");
    return{
      'times' : times,
      'name' : i.name,
      'singer' : i.artist,
      'album' : i.album,
      'id' : i.rid,
      'img':i.albumpic,
      'from':'kuwo'
    }
  })
  return r
}
function dealSearchSingerResult(data) {

  //console.log(data)
  let result = data || {artistCount:0,artists:[{}]}
  let map = result.map((r) => {

    let name = replaceA(replaceA(replaceA(r.name,'&',''),'nbsp',' '),'; ;',';')

    return {
      id:r.id,
      name: name,
      img: r.pic,
      from:'kuwo'
    }
  });
  return map
}
function replaceA(s,o,t){
  while(s.indexOf(o) !== -1){
    s = s.replace(o,t)
  }
  return s;
}
export async function getKwPlayListDetail(id,f){

  await musicList.prototype.getList({pid:id,pn:'1',rn:'100'}).then((r) => {

    ////console.log(r.data.data)
    let result = dealDetailResult(r.data)
    ////console.log(result)
    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function dealDetailResult(data){
  ////console.log(data)
  let {userName:user,img:img,listencnt:playCount,name:name,id:id,info:description} = data;
  let subscribedCount = ""

  let tags = data.tag.split(",")
  let tracks = data.musicList;
  let r = tracks.map((i) => {

    return{
      'times' : i.songTimeMinutes,
      'name' : i.name,
      'singer' : i.artist,
      'album' : i.album,
      'id' : i.rid,
      'img':i.albumpic,
      from:'kuwo'
    }
  })


  return {
    user:user,
    tags:tags,
    name:name,
    id:id,
    description:description,
    img:img,
    playCount:playCount,
    subscribedCount:subscribedCount,
    list:r,
    from:'kuwo'
  }
}
export async function getKwAlbumDetail(id,f){
  await albumInfo.prototype.getList({albumId:id,
    pn:1,
    rn:100}).then((r) => {

    ////console.log(r.data)
    let result = dealAlbumDetailResult(r)
    f(result)
  }).catch((r) => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function dealAlbumDetailResult(data){
  let list = data.data || {musicList:[]}
  let tracks = list.musicList;
  let r = tracks.map((i) => {

    return{
      'times' : i.songTimeMinutes,
      'name' : i.name,
      'singer' : i.artist,
      'album' : i.album,
      'id' : i.rid,
      'img':i.albumpic,
      from:'kuwo'
    }
  })


  return r;
}
export async function getKwSongsBySinger(id,page,perpage,f){
  await singer.prototype.getArtistMusic({artistid:id,
    pn:page,
    rn:perpage}).then((r) => {
    //console.log(r);
    let result = dealSongBySinger(r.data);
    f(result);
  }).catch((r) => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}

function dealSongBySinger(data){
  let list = data.list;
  let result = list.map((i) => {
    return{
      'times' : i.songTimeMinutes,
      'name' : i.name,
      'singer' : i.artist,
      'album' : i.album,
      'id' : i.rid,
      'img':i.albumpic,
      from:'kuwo'
    }

  })
  return {v:result,count:data.total};
}


export async function getKwPlayMusic(id,f){
  await playUrl.prototype.getPlayUrl({mid:id,type:'music'}).then((r) => {

    //console.log(r)
    if(!r.data){
      f(-1)
    }else {
      let result = dealPlayMusicResult(r)
      ////console.log(result)
      f(result)
    }
  }).catch((r) => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
/**
 * {
 *                     id: "1",
 *                     title: "打上花火",
 *                     info: "DAOKO,米津玄師",
 *                     // resource: require("./resource/DAOKO,米津玄師 - 打上花火.mp3"),
 *                     resource: "",
 *                     time: "04:50",
 *                     img:
 *                         "http://singerimg.kugou.com/uploadpic/softhead/400/20160913/20160913140233132.jpg"
 *                 },
 */
function dealPlayMusicResult(data){

  return {

    resource:data.data.url,

  }
}
export async function getKwlyric(id,f){
  await lrc.prototype.LrcRes(id).then((r) => {
    //console.log(r.data)
    let result = deallyricResult(r.data)
    while(result.indexOf('\r') !== -1){
      result = result.replace('\r','');
    }
    f(result)
  }).catch((r) => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function deallyricResult(data){

  let list = data.lrclist || []
  let result = '';
  list.map((r) => {
    let time = dealTime(r.time);
    ////console.log(time)
    result += time+r.lineLyric+"\n";
  })
  ////console.log(result)

  return result;

}

function dealTime(time){
  let fen = parseInt(time/60)
  let miao = parseInt(time) - fen * 60;
  let haomiao = (time - parseInt(time))
  if(fen < 10){
    fen = '0'+fen;
  }
  if(miao<10){
    miao = '0'+miao;
  }
  haomiao = parseInt(haomiao*100);
  if(haomiao<10){
    haomiao = '0'+haomiao;
  }
  return '['+fen+':'+miao+"."+haomiao+']';
}
